<template>
    <div>
      <table-expand
        ref="table"
        :columns="columns"
        :data="basicData"
        :loading="loading"
        :config="config"
        :pageSizeOpts="pageSizeOpts"
        :pageNavigation="pageNavigation"
        :queryFormItem="queryFormItem"
        :collapseFormItem="collapseFormItem"
        @loadPage="loadPage"
        @handleCreate="handleCreate"
        @handleDetail="handleDetail"
        @handleEdit="handleEdit"
        @handleDelete="handleDelete"
        @handleDeleteMultiple="handleDeleteMultiple"
        @handleSearch="handleSearch"
        @handleReset="handleReset"
      ></table-expand>
      <new-add-modal
        v-bind="addNewPanel"
        :modelType="modelType"
        :columns="formColumns"
        :rules="rules"
        @submit="handleNewAddSubmit"
      ></new-add-modal>
      <edit-modal
        v-bind="editPanel"
        :modelType="modelType"
        :columns="formColumns"
        :rules="rules"
        @submit="handleEditSubmit"
      ></edit-modal>
      <read-modal
        v-bind="readPanel"
        :modelType="modelType"
        :columns="columns"
        :rules="rules"
      ></read-modal>
    </div>
</template>

  <script>
    import tableExpand from '@/pages/components/tableExpand.vue';
    import newAddModal from '@/pages/components/newAddModal.vue';
    import editModal from '@/pages/components/editModal.vue';
    import readModal from '@/pages/components/readModal.vue';
    import logicDictionaryMixin from '@/pages/components/mixins/logicDictionaryMixin'
    import columnsMixin from './config'
    import { typeClassName } from '@/pages/config/classNameMap';
    import { cloneDeep } from 'lodash';
    export default {
        components: { tableExpand, newAddModal, editModal, readModal },
        name: 'job',
        mixins: [logicDictionaryMixin,columnsMixin],
        data () {
            return {
                classObject: null, // 单个实例
                className: typeClassName['job'].className,
                type: typeClassName['job'].type,
                modelType: typeClassName['job'].modelType,
                deptObject: null, // 所有部门实例
                departmentOptions: [], // 部门名称，下拉框使用
                jobOptions: [], // 岗位名称，下拉框使用
                seniorJobOptions: [], // 上级岗位名称，下拉框使用
                roleList: [], // 角色信息
                addNewPanel: {
                    width: 1024,
                    zIndex: 100,
                    titleStyle: {},
                    drawer: {
                        show: false
                    },
                    cols: 2
                },
                editPanel: {
                    width: 1024,
                    zIndex: 100,
                    titleStyle: {},
                    drawer: {
                        show: false
                    },
                    cols: 2,
                    data: {}
                },
                readPanel: {
                    width: 1024,
                    zIndex: 100,
                    titleStyle: {},
                    drawer: {
                        show: false
                    },
                    cols: 2,
                    data: {}
                },
                rules: {
                    sf_code: [
                        { required: true, message: '不能为空', trigger: 'blur' },
                        { max: 50, message: '编码最大长度为50', trigger: 'blur' }
                    ],
                    sf_name: [
                        { required: true, message: '不能为空', trigger: 'blur' },
                        { max: 200, message: '名称最大长度为200', trigger: 'blur' }
                    ],
                    departmentName: [
                        { required: true, message: '不能为空', trigger: 'blur', type:'array' }
                    ],
                    remark: [
                        { max: 500, message: '描述长度不可以超过500', trigger: 'blur' }
                    ]
                },
                loading: false,
                config: {
                    search: true,
                    addButton: true,
                    batchDeleteButton: true,
                    tableSizeIcon: true,
                    fullscreenIcon: true,
                    refreshIcon: false,
                    columnSettingIcon: true,
                    action: true,
                    additionalAction: false,
                    hidePage: true
                },
                pageSizeOpts: [10, 30, 50, 100],
                pageNavigation: { total: 0, pageSize: 20, pageNumber: 1 },
                basicData: [],
            };
        },
        computed: {
            additionalAction () {
                return [];
            }
        },
        created () {
            this.classObject = this.$modelObject.create(this.modelType);
            this.deptObject = this.$modelObject.create(`${typeClassName['dept'].modelType}`);
            this.loadPage();
            this.getdepartment()
            this.setOptionsByHier('job', this.classObject, 'jobOptions', {
                label: 'sf_name', value: 'sf_code'
            })
            this.getRoleData()
        },
        watch:{
            'addNewPanel.drawer.show': function (show) {
                if (show) {
                    this.seniorJobOptions = this.jobOptions;
                }
            },
            'editPanel.data.values': {
                handler (values) {
                    this.getSeniorOption(values)
                },
                deep: true
            }
        },
        methods: {
            // 获取所有部门信息
            getdepartment () {
                let filter = {
                    className: typeClassName['dept'].className,
                    type: typeClassName['dept'].type
                }
                let newFilter = '';
                if (this.tools.isReal(filter)) {
                    newFilter = this.deptObject.util.getFilter(filter);
                }
                this.deptObject
                    .query(newFilter)
                    .then((flag) => {
                        if (flag) {
                            let departmentOptions = [];
                            if (!!this.deptObject.record && !!this.deptObject.record.hier && !!this.deptObject.record.hier[0]) {
                                departmentOptions = this.getChildren('root', this.deptObject.record.hier[0].basic)
                            }
                            this.departmentOptions = departmentOptions;
                        } else {
                            this.departmentOptions = [];
                        }
                });
            },
            getChildren (parentId, data = this.data) {
                let children = [];
                let list = data.filter(item => item.parentId === parentId);
                list.map(item => {
                    if (!!item.pointer && item.pointer.className === typeClassName['person'].className) {
                        return;
                    }
                    children.push({
                        title: item.values.sf_name ? `${item.values.sf_name}` : item.name,
                        label: item.values.sf_name ? `${item.values.sf_name}` : item.name,
                        id: item.id,
                        value: item.id,
                        code: item.values.sf_code,
                        parentId: item.parentId,
                        contextmenu: true,
                        data: item,
                        pointer: item.pointer,
                        children: this.getChildren(item.id, data),
                        disabled: !!item.pointer && item.pointer.className === typeClassName['person'].className
                    })
                });
                return children;
            },
            //获取角色列信息
            getRoleData () {
                this.loading = true;
                // 下面的 params 是获取的表单查询参数
                this.pageNavigation.total = 0;
                let condition = "";
                this.$api.Role.queryByPage(this.pageNavigation.pageNumber, this.pageNavigation.pageSize, {condition}).then(res => {
                    let list = [];
                    list = cloneDeep(res.records);
                    this.pageNavigation.total = res.recordCount;
                    this.pageNavigation.pageNumber = res.pageNumber;
                    this.pageNavigation.pageSize = res.pageSize;
                    this.roleList = list.map(d => {
                      return {
                                label: d.roleName === null ? '' : d.roleName,
                                value: d.roleCode === null ? '' : d.roleCode
                            }
                    })
                }).finally(() => {
                    this.loading = false;
                })
            },
            /**
             * 查看框数据的处理
             */
            readDataCondition(data){
                data.heightCode = this.getOptionValue(this.jobOptions, data.heightName,'value')
                data.heightName = this.getOptionValue(this.jobOptions, data.heightName)
                data.roleCode = this.getOptionValueByArray(this.roleList, data.roleName,'value')
                data.departmentCode = this.getOptionValueByCascader(this.departmentOptions, data.departmentName,'code')
            },
            // 上级岗位信息不能选自己
            getSeniorOption(values){
               this.seniorJobOptions =  this.jobOptions.filter(d=>d.value!==values.sf_code)
            }
        }
    };
  </script>
  <style scoped lang="scss"></style>
